/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication3;

import java.io.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 *
 * @author andrea
 * @version 1
 */
public class Main {

    private static final String DOMAIN = "ts3.travian.it";
    private static final String AUTH_URL = "http://" + DOMAIN + "/dorf1.php";
    public static String pageHTML;
    private static Map<String, Integer> resources = new HashMap<String, Integer>();

    /**
     *
     * @param args
     */
    public static void main(String[] args) {
        try {
            connect("mazzi.andrea3@gmail.com", "moimacco");
        } catch (ClientProtocolException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    /**
     *
     *
     * @param login
     * @param password
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static void connect(String login, String password) throws ClientProtocolException, IOException {

        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpConnection httpConn = new HttpConnection(httpclient);

        Map<String, String> params = new HashMap<String, String>();
        params.put("s1", "Login");
        params.put("w", "1366:768");
        params.put("login", "1325103450");
        httpConn.post(AUTH_URL, params);
        if (httpConn.getEntity() != null) {
            pageHTML = convertStreamToString(httpConn.getEntity().getContent());
        }


        //      httpConn.get("http://ts3.travian.it/build.php?id=20", params);

        httpConn.post("http://ts3.travian.it/dorf2.php?d=10914502&a=0&c=4c9e1e", params);
        if (httpConn.getEntity() != null) {
            pageHTML = convertStreamToString(httpConn.getEntity().getContent());
          

       /*     File f;
            f = new File("myfile.php");
            if (!f.exists()) {
                f.createNewFile();


                try {
                    //crea il file
                    FileOutputStream file = new FileOutputStream(f);
                    PrintStream Output = new PrintStream(file);
                    Output.print(pageHTML);
                } catch (IOException e) {
                    System.out.println("Errore: " + e);
                    System.exit(1);
                }
            }
         */



        }
        /*ricavo le info di base
        ScanningInformazioni testo = new ScanningInformazioni(pageHTML);
        testo.ricavaOra();
        testo.ricavaInfo();
        testo.ricavaLivelloRisorse();
        //manipola*/
    }

    /**
     *
     * @param is
     * @return
     */
    public static String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        String NL = System.getProperty("line.separator");
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line).append(NL);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}
